package com.woniu.micropropertyserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.microcommonsserver.entity.HouseInfoEntity;
import com.woniu.microcommonsserver.entity.OwnerInfoEntity;

import com.woniu.microcommonsserver.entity.ParkCarInfoEntity;
import com.woniu.micropropertyserver.housemag.service.IHouseService;
import com.woniu.micropropertyserver.mapper.OwnerMapper;
import com.woniu.micropropertyserver.parkmag.service.IParkService;
import com.woniu.micropropertyserver.service.IOwnerInfoService;
import com.woniu.micropropertyserver.util.PageRest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class OwnerInfoServiceImpl implements IOwnerInfoService {

    @Resource
    private OwnerMapper ownerMapper;

    @Resource
    private IHouseService houseServiceImpl;
    @Resource
    private IParkService parkServiceImpl;

    @Override
    public List<OwnerInfoEntity> findall() {
        System.out.println("查询所有");
        return ownerMapper.findall();
    }
    @Transactional(isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    @Override
    public PageInfo<OwnerInfoEntity> findAllByParams(PageRest pageRest, OwnerInfoEntity ownerInfoEntity) {
        System.out.println(ownerInfoEntity);
        System.out.println(ownerMapper.findAllByParams(pageRest,ownerInfoEntity));
        System.out.println("================");
        return PageHelper.startPage(pageRest.getPageNum(),pageRest.getPageSize()).doSelectPageInfo(()->ownerMapper.findAllByParams(pageRest,ownerInfoEntity));
    }

    @Override
    public OwnerInfoEntity findByCard(String id) {
        return ownerMapper.findByCard(id);
    }

    @Override
    public OwnerInfoEntity findByNameAndHouse(String name,String houseNum) {
        return ownerMapper.findByNameAndHouse(name, houseNum);
    }

    @Override
    public OwnerInfoEntity findById(Long id) {

        OwnerInfoEntity ownerInfoEntity = ownerMapper.findById(id);
        String cardId=ownerInfoEntity.getCardId();
         List<HouseInfoEntity>  houes=houseServiceImpl.findByCard(cardId);
        ownerInfoEntity.setHouseInfos(houes);
        List<ParkCarInfoEntity> parks=parkServiceImpl.findParksByCard(cardId);
        ownerInfoEntity.setParkCarInfos(parks);
        return ownerInfoEntity;
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    public int updateInfo(OwnerInfoEntity ownerInfo) {
        return ownerMapper.updateInfo(ownerInfo);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    public int delInfo(Long id) {
        return ownerMapper.delInfo(id);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    public Long saveInfo(OwnerInfoEntity ownerInfo) {

        return ownerMapper.saveInfo(ownerInfo);
    }


    /**
     * 根据 房间id，查询它的业主
     * @param sid 房间id
     * @return
     */
    @Override
    public OwnerInfoEntity findOwerBySid(Long sid){
        return ownerMapper.findOwerBySid(sid);
    }

    /**
     * 根据 业主id，查询业主名字
     * @param id
     * @return
     */
    @Override
    public String findOwerNameById(Long id){
        return ownerMapper.findOwerNameById(id);
    }

    @Override
    public List<OwnerInfoEntity> findByName(String name) {
        return null;
    }

    @Override
    public OwnerInfoEntity findHouseById(Long id) {
        return ownerMapper.findHouseById(id);
    }

    /**
     * 根据 房间id，查询它的业主id
     * @param sid
     * @return
     */
    @Override
    public Long findOwerIdBySid(Long sid){
        return ownerMapper.findOwerIdBySid(sid);
    }

}
